package cn.echcz.restboot.config;

import com.auth0.jwt.algorithms.Algorithm;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "security.jwt")
public class JwtProperties {
    @Getter @Setter
    private Item access = new Item();
    @Getter @Setter
    private Item refresh = new Item();
    {
        access.setType("access");
        access.setMaxAge(180000);
        refresh.setType("refresh");
        refresh.setMaxAge(86400000);
    }

    @NoArgsConstructor
    public static class Item {
        /**
         * 加密安全码
         */
        @Getter
        private String secret = "cn.echcz.restboot";
        /**
         * 签发人
         */
        @Getter @Setter
        private String issuer = "echcz";
        /**
         * token类型
         */
        @Getter @Setter
        private String type;
        /**
         * 有效时间(单位为ms)
         */
        @Getter @Setter
        private long maxAge;
        /**
         * 加密算法
         */
        @Getter
        private Algorithm algorithm = Algorithm.HMAC256(this.secret);

        public void setSecret(String secret) {
            this.secret = secret;
            this.algorithm = Algorithm.HMAC256(this.secret);
        }
    }
}
